import {messages} from "share/common";
import React from 'react'
import {connect} from 'react-redux'
import { message, Spin, Row, Col, Table, Badge, Popover, Radio } from 'antd'
import 'styles/components/honest-view.scss'
import baseService from 'share/base.service'
import constants from "share/constants"
const RadioGroup = Radio.Group;
const RadioButton = Radio.Button;

class HonestView extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      honestInfo: {},
      headLoading: false,
      tableLoading: false,
      operationType: null,
      businessCode: null,
      pagination: {
        pageSize: 5,
        total: 0
      },
      page: 0,
      size: 5,
      data:[],
      columns: [
        {title: messages('components.key752')/*相关单据*/,dataIndex: 'businessCode'},
        {title: messages('components.key753')/*事件描叙*/,dataIndex: 'comment', width: '18%', render: value => <Popover placement="topLeft" content={value}>{value}</Popover>},
        {title: messages('components.key754')/*信用规则*/,dataIndex: 'ruleCondition',width:'10%', render: value => <Popover placement="topLeft" content={value}>{value}</Popover>},
        {title: messages('components.key755')/*信用类型*/,dataIndex: 'modelTypeId', align:'center',render: value => {
            let res = '-';
            switch (value){
              case 1001:
                res = messages('components.key756')/*报销信用*/;
                break;
              default:
                res = messages('components.key757')/*审批信用*/;
                break;
            }
            return res;
          }},
        {title: messages('components.key758')/*处理方式*/,dataIndex: 'operationType', align:'center', render: value => {
            let res = '-';
            switch (value){
              case 1001:
                res = messages('components.key741')/*加分*/;
                break;
              case 1002:
                res = messages('components.key742')/*减分*/;
                break;
              case 1003:
                res = messages('components.key743')/*升级*/;
                break;
              default:
                res = messages('components.key744')/*降级*/;
                break;
            }
            return res;
          }},
        {title: messages('components.key759')/*处理结果*/,dataIndex: 'change', align:'center',render: (value,record) => {
            let res = '-';
            switch (record.operationType){
              case 1001:
                res = `${record.changeScore}`;
                break;
              case 1002:
                res = `-${record.changeScore}`;
                break;
              case 1003:
                res = `${record.changeRank}`;
                break;
              default:
                res = `-${record.changeRank}`;
                break;
            }
            return res;
          }},
        {title: messages('components.key760')/*更新时间*/,dataIndex: 'lastModifiedDate', render: value => new Date(value).format('yyyy-MM-dd'), width: 120},
        {title: messages('common.column.status')/*状态*/, dataIndex: 'status', render: value => {
            let temp = value === 1001;
            return <Badge status={temp ? 'success' : 'error'} text={temp ? messages('common.status.enable')/*启用*/ : messages('common.reject')/*驳回*/}/>;
          }}
      ]
    }
  }

  componentWillMount() {
    const { applicant, jobId } = this.props;
    const { page, size } = this.state;
    let params = {
      userOID: applicant.userOID,
      page,
      size
    };
    jobId && (params.jobId = jobId);
    this.setState({headLoading: true, tableLoading: true});
    Promise.all([
      baseService.getHonestInfo(applicant.userOID, jobId),
      baseService.getHonestHistory(params)
    ]).then(res => {
      this.setState({
        data: res[1].data,
        tableLoading: false,
        headLoading: false,
        honestInfo: res[0].data,
        pagination: {
          pageSize: 5,
          total: Number(res[1].headers['x-total-count']),
          onChange: this.onChangePager,
          current: this.state.page + 1
        }
      });
    }).catch(() => {
      this.setState({headLoading: false,tableLoading: false});
      message.error(messages('components.key761')/*系统异常*/);
    });
  }

  getList = () => {
    const { page, size, operationType, businessCode} = this.state;
    const { applicant, jobId } = this.props;
    let params = {
      userOID: applicant.userOID,
      page,
      size
    };
    jobId && (params.jobId = jobId);
    operationType && (params.operationType = operationType);
    businessCode && businessCode !== '' && (params.businessCode = businessCode);
    this.setState({tableLoading: true});
    baseService.getHonestHistory(params).then(res => {
      this.setState({
        data: res.data,
        tableLoading: false,
        pagination: {
          pageSize: 5,
          total: Number(res.headers['x-total-count']),
          onChange: this.onChangePager,
          current: this.state.page + 1
        }
      });
    }).catch(e => {
      this.setState({tableLoading: false});
      message.error(e.response.data.message);
    });
  };

  onChangePager = (page) => {
    if(page - 1 !== this.state.page)
      this.setState({page: page - 1,}, this.getList)
  };

  handleChangeRadio = e => {
    this.setState({ operationType: e.target.value, page: 0 }, this.getList)
  };

  render(){
    const { applicant } = this.props;
    const { honestInfo, headLoading, columns, data, pagination, tableLoading } = this.state;
    return(
      <div className="honest-view">
        <h4 className="title">{messages('components.key762')/*员工消息*/}</h4>
        <Spin spinning={headLoading} className='header-info'>
          <Row>
            <Col span={3}>{messages('components.key763')/*员工*/}：</Col>
            <Col span={9}>{applicant.fullName}</Col>
            <Col span={3}>{messages('components.key420')/*工号*/}：</Col>
            <Col span={9}>{applicant.employeeID}</Col>
          </Row>
          <Row>
            <Col span={3}>{messages('components.key764')/*信用分*/}：</Col>
            <Col span={9}>{honestInfo.score}</Col>
            {
              honestInfo.rankFlag && (
                <span>
                  <Col span={3}>{messages('components.key765')/*信用等级*/}：</Col>
                  <Col span={9}>{honestInfo.creditRank}</Col>
                </span>
              )
            }
          </Row>
          {
            honestInfo.rankFlag && (
              <Row style={{height:'auto'}}>
                <Col span={3}>{messages('components.key766')/*等级说明*/}：</Col>
                <Col span={21}>{honestInfo.rankComment}</Col>
              </Row>
            )
          }
        </Spin>
        <h4 className="title">{messages('components.key706')/*信用事件*/}</h4>
        <div className="table-title">
          <RadioGroup defaultValue={undefined} onChange={this.handleChangeRadio}>
            <RadioButton value={undefined}>{messages('components.key223')/*全部*/}</RadioButton>
            {constants.creditRuleOperation.map(item => <RadioButton value={item.value} key={item.value}>{item.text}</RadioButton>)}
          </RadioGroup>
        </div>
        <div style={{height: '286px'}}>
          <Table bordered
                 dataSource={data}
                 size={'small'}
                 pagination={pagination}
                 columns={columns}
                 rowKey="id"
                 loading={tableLoading}/>
        </div>
      </div>
    )
  }
}

HonestView.propTypes = {
  applicant: React.PropTypes.object, //用户信息
  jobId: React.PropTypes.string //岗位id
};

HonestView.defaultProps = {
  applicant: {}
};

export default connect()(HonestView);
